﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.Presentations.FormBase;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Presentations;
using STLGROUP.Layers.Common;
using STLGROUP.Layers.BLL;

namespace QuanLyBanHang
{
    /// <summary>
    /// MF001- Quan ly khach hang
    /// </summary>
    /// <author>viet sang</author>
    /// <date>10/02/2012</date>
    public partial class MF001 : frmManager
    {
        #region Variables

        DT_KhachHangBLL _business;
        private static int _idKhachHang;
        public static bool _isShowButtonChon = false;

        #endregion

        #region Constructors

        public MF001()
        {
            InitializeComponent();

            InitializeForm();
        }

        #endregion

        #region Protected methods

        protected override void LoadData()
        {
            this._bindingSource.DataSource = Cache.Context.DT_KhachHang;//_business.GetData();
            this.dgvKhachHang.DataSource = _bindingSource;
            base.LoadData();
        }

        protected override void ImportFormExcel()
        {
            ImportDataFromExcel<DT_KhachHang>(dgvKhachHang);
        }

        protected override void ExportToExcel()
        {
            ExportDataToExcel(dgvKhachHang);
        }

        protected override void AddDataBinding()
        {
            txtMaKhachHang.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "MaKhachHang", true);
            txtHoDem.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "HoDem", true);
            txtTen.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Ten", true);
            dtiNgaySinh.DataBindings.Add(Const.BIND_VALUE, _bindingSource, "NgaySinh", true);
            chbxGioiTinh.DataBindings.Add(Const.BIND_CHECKED, _bindingSource, "GioiTinh", true);
            txtEmail.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Email", true);
            txtSoCMND.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "SoCMND", true);
            txtSoDienThoai.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "SoDienThoai", true);
            txtDiaChi.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "DiaChi", true);
            txtFax.DataBindings.Add(Const.BIND_TEXT, _bindingSource, "Fax", true);

            cboTinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            cboTinhThanh.ValueMember = "IDTinhThanh";
            cboTinhThanh.DisplayMember = "TenTinhThanh";
            cboTinhThanh.DataBindings.Add(Const.BIND_SELECTED_VALUE, _bindingSource, "IDTinhThanh", true);

            TinhThanh.DataSource = Cache.Context.DM_TinhThanh;
            TinhThanh.ValueMember = "IDTinhThanh";
            TinhThanh.DisplayMember = "TenTinhThanh";

            base.AddDataBinding();
        }

        protected override void RefreshData()
        {
            base.RefreshData();
            UICommon.ShowDataNotFound(dgvKhachHang, lblTimKiem, false);
        }

        protected override void AddNew()
        {
            base.AddNew();
            chbxGioiTinh.Checked = false;
            txtHoDem.Focus(); 
        }

        protected override int UpdateData()
        {
            if (base.UpdateData() > 0)
            {
                IQueryable<DT_KhachHang> lstKH = this._bindingSource.DataSource as IQueryable<DT_KhachHang>;
                _business.UpdateKhachHang(lstKH);
                return base.UpdateData();
            }            
            LoadData();

            return -1;
        }

        protected override void Search()
        {
            var data = from c in Cache.Context.DT_KhachHang
                       where c.HoDem.Contains(tscbxTimKiem.Text) ||
                       c.Ten.Contains(tscbxTimKiem.Text) ||
                       c.DiaChi.Contains(tscbxTimKiem.Text) ||
                       c.Email.Contains(tscbxTimKiem.Text) ||
                       c.SoDienThoai.Contains(tscbxTimKiem.Text) ||
                       c.SoCMND.Contains(tscbxTimKiem.Text)
                       select c;

            List<DT_KhachHang> lstData = data.ToList<DT_KhachHang>();

            //Hien thi thong bao khi khong tim thay du lieu
            UICommon.ShowDataNotFound(dgvKhachHang, lblTimKiem, lstData.Count == 0);

            _bindingSource.DataSource = data;
            dgvKhachHang.DataSource = _bindingSource;

        }

        #endregion

        #region Private methods

        private void InitializeForm()
        {
            if (_isShowButtonChon)
            {
                DataGridViewButtonColumn colButtonChon = new DataGridViewButtonColumn();
                colButtonChon.HeaderText = "Lựa chọn";
                colButtonChon.Text = "Chọn";
                colButtonChon.UseColumnTextForButtonValue = true;
                colButtonChon.Width = 80;

                dgvKhachHang.Columns.Add(colButtonChon);
            }

            _business = new DT_KhachHangBLL();

            UICommon.AddTrackingColumn(this.dgvKhachHang);
            this.dgvKhachHang.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dgvKhachHang_RowPostPaint);
            this.txtMaKhachHang.ReadOnly = true;
        }

        #endregion

        #region Public Methods

        public static int GetIDKhachHang()
        {
            return _idKhachHang;
        }

        #endregion

        #region Events

        private void dgvKhachHang_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void dgvKhachHang_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = (DataGridView)sender;
            // Current row record
            string rowNumber = (e.RowIndex + 1).ToString();

            // Format row based on number of records displayed by using leading zeros
            while (rowNumber.Length < dg.RowCount.ToString().Length)
            {
                rowNumber = "0" + rowNumber;
            }

            // Position text
            SizeF size = e.Graphics.MeasureString(rowNumber, this.Font);
            if (dg.RowHeadersWidth < (int)(size.Width + 20))
            {
                dg.RowHeadersWidth = (int)(size.Width + 20);
            }

            // Use default system text brush
            Brush b = SystemBrushes.ControlText;

            // Draw row number
            e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        private void btnTinhThanh_Click(object sender, EventArgs e)
        {
            UICommon.ShowPopup(new CF002(), StructsEnums.EnumDanhMuc.TinhThanh,cboTinhThanh,TinhThanh);
        }

        //Add by Van Long - date: 30/04/2012
        private void dgvKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == Const.INDEX_COLUMN_CHON_KHACH_HANG)
                {
                    _idKhachHang = ((DT_KhachHang)(_bindingSource.Current)).IDKhachHang;

                    this.Close();
                }
                else
                {
                    _idKhachHang = 0;
                }
            }
            catch
            {
                _idKhachHang = 0;
            }
        }

        #endregion
    }
}
